package com.yuyou.fn.educational.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.ClassTime;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.service.IClassTimeService;
import com.yuyou.fn.educational.service.ICourseClassService;
import org.springframework.stereotype.Service;

import com.yuyou.fn.educational.mapper.RoomTimeMapper;
import com.yuyou.fn.educational.entity.RoomTime;
import com.yuyou.fn.educational.service.IRoomTimeService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * RoomTime 表数据服务层接口实现类
 *
 */
@Service
public class RoomTimeServiceImpl extends SuperServiceImpl<RoomTimeMapper, RoomTime,Long> implements IRoomTimeService {


    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private IClassTimeService classTimeService;


    @Override
    public void saveOrUpdateRoomTime(Long classId) {
        Example<RoomTime> example = RoomTime.createExample();
        example.createCriteria().andEqualTo("classId",classId);
        deleteByExample(example);
        CourseClass courseClass = courseClassService.findById(classId);
        List<ClassTime> classTimeList = classTimeService.findClassTimeByClassId(classId);
        List<RoomTime> roomTimeList = new ArrayList<>(classTimeList.size());
        Date now = TimeUtils.now();
        SimpleDateFormat dateSDF = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for(ClassTime classTime : classTimeList ){
            RoomTime roomTime = new RoomTime();
            roomTime.setClassId(classId);
            roomTime.setCreateTime(now);
            roomTime.setUpdateTime(now);
            roomTime.setRoomId(courseClass.getRoomId());
            roomTime.setSubject(courseClass.getClassName()+"【第"+classTime.getLectureNo()+"讲】");
            String date = dateSDF.format(classTime.getClassDate());

            try {
                roomTime.setStart(timeSDF.parse(date +" " + classTime.getStartTime() ));
                roomTime.setEnd(timeSDF.parse(date +" " + classTime.getEndTime() ));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

            roomTimeList.add(roomTime);
        }
        insertBatch(roomTimeList);
    }

    @Override
    public List<RoomTime> findRoomTimeByRoomId(Long roomId, Date start, Date end, Long busTeamId) {
        Example<RoomTime> example = RoomTime.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roomId" ,roomId );
//        criteria.andEqualTo("busTeamId" ,busTeamId);
        criteria.andBetween("start",start,end);
        return findListByExample(example);
    }

    @Override
    public void detectConflict(Long classId, Long roomId) {
        List<ClassTime> classTimeList = classTimeService.findClassTimeByClassId(classId);
        SimpleDateFormat dateSDF = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for(ClassTime classTime : classTimeList ){

            String date = dateSDF.format(classTime.getClassDate());
            Date start ;
            Date end ;
            try {
                start = timeSDF.parse(date +" " + classTime.getStartTime() );
                end = timeSDF.parse(date +" " + classTime.getEndTime() );
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            Example<RoomTime> example = RoomTime.createExample();
            example.createCriteria().andEqualTo("roomId",roomId ).andBetween("start",start ,end );
            example.or().andEqualTo("roomId",roomId ).andBetween("end",start,end);
            long l = countByExample(example);
            if(l > 0 ){
                throw new BusException("【第"+classTime.getLectureNo()+"讲】时间有冲突");
            }
        }
    }
}